import { areadata, getAllAreaData } from "@/api";

export default {
  addrMap: [],

  async getAddrMap() {
    let res = await getAllAreaData();
    this.addrMap = res.data.data;
  },

  //
  getLactionStr(options) {
    let strLocation = "";

    options.forEach(element => {
      let temp = this.getIDFromName(element);
      strLocation += temp + ",";
    });
    return strLocation.substr(0, strLocation.length - 1);
  },

  getIDFromName(org) {
    if (org == "中国") {
      return 3633;
    } else if (org == "海外") {
      return 3634;
    } else {
      for (var i = 0; i < this.addrMap.length; i++) {
        if (org == this.addrMap[i].name) {
          return this.addrMap[i].id;
        }
      }
    }
    return org;
  },

  async areadata(selectIndex, id) {
    let retArea = [];
    let res = await areadata(id);

    if (selectIndex == 0) {
      retArea = res.data.data;
    } else if (selectIndex == 1) {
      retArea = res.data.data[0].children;
    } else if (selectIndex == 2) {
      retArea = res.data.data[0].children;
    }

    return retArea;
  },

  async getAllOptions(location) {
    //
    if (!location) return null;
    let arrLocation = location ? location.split(",") : location;

    //xxl getAllOptions

    let res0 = await areadata(0);
    this.placeOptionsAll[0] = res0.data.data;

    if (arrLocation.length > 0) {
      let level1 = this.getLevelIDFromIDS(location, 0);
      if (level1 != "undefined") {
        let res1 = await areadata(level1);
        this.placeOptionsAll[1] = res1.data.data[0].children;
      }
      //console.log(typeof(level1));

      let level2 = this.getLevelIDFromIDS(location, 1);
      if (level2 != "undefined") {
        let res2 = await areadata(level2);
        this.placeOptionsAll[2] = res2.data.data[0].children;
      }
      //console.log(typeof(level2));
    }

    return this.placeOptionsAll;
  },

  getAllLevelNames(location) {
    this.allLevelNames = [];
    let levelName0 = this.getLevelNameFromIDS(location, 0);
    this.allLevelNames.push(levelName0);

    let levelName1 = this.getLevelNameFromIDS(location, 1);
    this.allLevelNames.push(levelName1);

    let levelName2 = this.getLevelNameFromIDS(location, 2);
    this.allLevelNames.push(levelName2);

    return this.allLevelNames;
  },

  getLevelNameFromIDS(location, index) {
    if (!location) return "";
    let arrLocation = location ? location.split(",") : location;
    if (arrLocation && arrLocation.length >= index + 1) {
      if (index == 0) {
        if (arrLocation[0] == "3633") {
          return "中国";
        } else if (arrLocation[0] == "3634") {
          return "海外";
        } else {
          return "";
        }
      } else {
        //console.log(this.addrMap);

        var areaItem = this.addrMap.filter((p) => {
          return p.id == arrLocation[index];
        });
        if(areaItem[0] != undefined){
          return areaItem[0].name;
        }else{
          return "";
        }


        // return this.addrMap[arrLocation[index]];
        //return "";
      }
    } else {
      return "";
    }
  },

  getLevelIDFromIDS(location, index) {
    let arrLocation = location.split(",");
    if (arrLocation.length >= index + 1) {
      return arrLocation[index];
    }
  },

  placeOptionsAll: [[], [], []],
  allLevelNames: []
};
